package com.fly.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fly.persistence.DataEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author Fly
 * @Description
 * @Date Created in 8:25 2018/1/25
 * @Modified by
 */
@Entity
@Table(name = "sys_user")
public class User extends DataEntity<User> implements UserDetails{

    @NotNull(message = "用户名不能为空")
    @Column(unique = true)
    @Size(min = 2,max = 32)
    private String username;

    private String password = "123456";

    private String nickname;

    /**
     * 用户所对应的角色
     */
    @ManyToMany(cascade = CascadeType.DETACH, fetch = FetchType.EAGER)
    @JoinTable(name = "sys_user_role", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
            inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"))
    private List<Role> roleList = new ArrayList<>();

    @Override
    public String getUsername() {
        return username;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    @JsonIgnore
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();
        authorities.add(new SimpleGrantedAuthority("admin"));
        return authorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }


    public List<Role> getRoleList() {
        return roleList;
    }

    /*public void setRoleList(List<Role> roleList) {
        this.roleList = roleList;
    }*/

    @JsonIgnore
    public List<Long> getRoleIdList() {
        List<Long> roleIdList = new ArrayList<>();
        for (Role role : roleList) {
            roleIdList.add(role.getId());
        }
        return roleIdList;
    }

    public void setRoleIdList(List<String> roleIdList) {
        roleList = new ArrayList<>();
        for (String roleId : roleIdList) {
            Role role = new Role();
            role.setId(Long.valueOf(roleId));
            roleList.add(role);
        }
    }

    public String getRoleIds() {
        return StringUtils.join(getRoleIdList(), ",");
    }

    public void setRoleIds(String roleIds) {
        roleList = new ArrayList<>();
        if (roleIds != null){
            String[] ids = StringUtils.split(roleIds, ",");
            setRoleIdList(Arrays.asList(ids));
        }
    }

    /**
     * 检查是否是管理员
     * @return
     */
    public boolean isAdmin(){
        return this.id == 1;
    }
}
